﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace UtilZ.Dotnet.WindowsDesktopEx.WPF
{
    /// <summary>
    /// null或空值NullOrEmpyToBoolConverter
    /// </summary>
    public class NumberToIsEnableConverter : DependencyObject, IValueConverter
    {
        /// <summary>
        /// 辅助文本依赖属性
        /// </summary>
        public static readonly DependencyProperty AssistTextProperty =
            DependencyProperty.Register(nameof(BoundaryValue), typeof(double), typeof(NumberToIsEnableConverter),
                new FrameworkPropertyMetadata(0d, new PropertyChangedCallback(OnPropertyChangedCallback)));

        private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selfControl = (NumberToIsEnableConverter)d;
        }

        /// <summary>
        /// 获取或设置比较边界值
        /// </summary>
        public double BoundaryValue
        {
            get
            {
                return (double)base.GetValue(AssistTextProperty);
            }
            set
            {
                base.SetValue(AssistTextProperty, value);
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public NumberToIsEnableConverter()
            : base()
        {

        }


        /// <summary>
        /// 内存对象中转换到显示
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return false;
            }

            var boundaryValue = this.BoundaryValue;
            if (value is byte)
            {
                return ((byte)value) > boundaryValue;
            }
            else if (value is short)
            {
                return ((short)value) > boundaryValue;
            }
            else if (value is ushort)
            {
                return ((ushort)value) > boundaryValue;
            }
            else if (value is int)
            {
                return ((int)value) > boundaryValue;
            }
            else if (value is uint)
            {
                return ((uint)value) > boundaryValue;
            }
            else if (value is long)
            {
                return ((long)value) > boundaryValue;
            }
            else if (value is ulong)
            {
                return ((ulong)value) > boundaryValue;
            }
            else if (value is float)
            {
                return ((float)value) > boundaryValue;
            }
            else if (value is double)
            {
                return ((double)value) > boundaryValue;
            }
            else if (value is decimal)
            {
                return ((decimal)value) > (decimal)boundaryValue;
            }
            else
            {
                return System.Convert.ToDouble(value) > boundaryValue;
            }
        }

        /// <summary>
        /// 显示转换回内存对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
